/**
 * common
 */

layui.define(function(exports) {
    let {layer, setter, admin} = layui, route = layui.router();

    //公共业务的逻辑处理可以写在此处，切换任何页面都会执行
    const path = admin.correctRouter(route.path.join("/")), token = AppGeneral.localStorage.getItem('TOKEN');
    if (path !== '/user/login' && !token) {
        layer.closeAll();
        return location.hash = "/user/login/redirect=" + encodeURIComponent(path);
    }

    //退出
    admin.events.logout = function() {
        layer.confirm('确认退出登录吗？', function(index) {
            //执行退出接口
            let requestIndex = layer.msg('正在处理请求...', { icon: 16, shade: 0.01, time: false });
            admin.req({
                url: setter.request.apiUrl + '/logout',
                headers: {
                    Authorization: setter.request.tokenType + token
                },
                type: 'post',
                done: function(result) {
                    layer.close(requestIndex);
                    if (result.code !== 200) {
                        return layer.msg('登出失败', {icon: 5});
                    }
                    layer.close(index);
                    layer.msg('登出成功', {icon: 6,time: 500}, function (index) {
                        localStorage.removeItem('TOKEN');
                        layer.close(index);
                        admin.exit();
                    });
                },
                error: function () {
                    layer.close(requestIndex);
                }
            });
        })
    };

    admin.events.cacheClear = function () {
        layer.confirm('清除缓存，是否继续？', {icon: 3, title:'提示'}, function(index) {
            localStorage.removeItem(setter.tableName);
            localStorage.removeItem('REMEMBER_ACCOUNT');
            layer.msg('清除成功');
            layer.close(index);
        })
    }
    //对外暴露的接口
    exports('common', {});
});